<?php

use think\migration\Migrator;
use think\migration\db\Column;

class CreateShopActivityGrouponTable extends Migrator
{
    /**
     * Migrate Up.
     */
    public function up()
    {
        $table = $this->table('shop_activity_groupon', ['comment' => '拼团', 'engine' => 'InnoDB', 'collation' => 'utf8mb4_general_ci']);
        $table->addColumn('user_id', 'integer', ['limit' => 11, 'default' => 0, 'comment' => '团长'])
            ->addColumn('goods_id', 'integer', ['limit' =>  11, 'default' => 0, 'comment' => '商品'])
            ->addColumn('activity_id', 'integer', ['limit' =>  11, 'default' => 0, 'comment' => '活动'])
            ->addColumn('num', 'integer', ['limit' =>  10, 'default' => 0, 'comment' => '成团人数'])
            ->addColumn('current_num', 'integer', ['limit' =>  10, 'default' => 0, 'comment' => '当前人数'])
            ->addColumn(Column::enum('status', ['invalid', 'ing', 'finish', 'finish_fictitious'])->setDefault('ing')->setComment('状态:invalid=已过期,ing=进行中,finish=已成团,finish_fictitious=虚拟成团'))
            ->addColumn('expire_time', 'integer', ['limit'  =>  10, 'null' => true, 'default' => null, 'comment' => '过期时间'])
            ->addColumn('finish_time', 'integer', ['limit'  =>  10, 'null' => true, 'default' => null, 'comment' => '成团时间'])
            ->addColumn('create_time', 'integer', ['limit'  =>  10, 'null' => true, 'default' => null, 'comment' => '创建时间'])
            ->addColumn('update_time', 'integer', ['limit'  => 10, 'null' => true, 'default' => null, 'comment' => '更新时间'])
            ->create();


        $table = $this->table('shop_activity_groupon_log', ['comment' => '参团记录', 'engine' => 'InnoDB', 'collation' => 'utf8mb4_general_ci']);
        $table->addColumn('user_id', 'integer', ['limit' =>  11, 'default' => 0, 'comment' => '用户'])
            ->addColumn('nickname', 'string', ['limit' => 255, 'null' => true, 'default' => null, 'comment' => '用户昵称'])
            ->addColumn('avatar', 'string', ['limit' => 255, 'null' => true, 'default' => null, 'comment' => '头像'])
            ->addColumn('groupon_id', 'integer', ['limit' =>  11, 'default' => 0, 'comment' => '团'])
            ->addColumn('goods_id', 'integer', ['limit' =>  11, 'default' => 0, 'comment' => '商品'])
            ->addColumn('goods_sku_price_id', 'integer', ['limit' =>  11, 'default' => 0, 'comment' => '商品规格'])
            ->addColumn('activity_id', 'integer', ['limit' =>  11, 'default' => 0, 'comment' => '活动'])
            ->addColumn(Column::tinyInteger('is_leader')->setDefault(0)->setUnsigned()->setComment('是否团长:0=不是,1=是'))
            ->addColumn(Column::tinyInteger('is_fictitious')->setDefault(0)->setUnsigned()->setComment('是否虚拟:0=不是,1=是'))
            ->addColumn('order_id', 'integer', ['limit' =>  11, 'default' => 0, 'comment' => '订单'])
            ->addColumn(Column::tinyInteger('is_refund')->setDefault(0)->setUnsigned()->setComment('是否退款:0=不是,1=是'))
            ->addColumn('create_time', 'integer', ['limit'  =>  10, 'null' => true, 'default' => null, 'comment' => '创建时间'])
            ->addColumn('update_time', 'integer', ['limit'  => 10, 'null' => true, 'default' => null, 'comment' => '更新时间'])
            ->addIndex('groupon_id')
            ->addIndex('user_id')
            ->create();
    }

    /**
     * Migrate Down.
     */
    public function down()
    {
        // 优惠券
        $table = $this->table('shop_activity_groupon');
        $table->drop();

        // 用户优惠券
        $table = $this->table('shop_activity_groupon_log');
        $table->drop();
    }
}
